package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.vo.ProSubjectVo;
import com.ruoyi.system.domain.bo.ProSubjectBo;
import com.ruoyi.system.service.IProSubjectService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目管理
 *
 * @author ruoyi
 * @date 2023-05-07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/subject")
public class ProSubjectController extends BaseController {

    private final IProSubjectService iProSubjectService;

    /**
     * 查询项目管理列表
     */
    @SaCheckPermission("system:subject:list")
    @GetMapping("/list")
    public TableDataInfo<ProSubjectVo> list(ProSubjectBo bo, PageQuery pageQuery) {
        return iProSubjectService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出项目管理列表
     */
    @SaCheckPermission("system:subject:export")
    @Log(title = "项目管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ProSubjectBo bo, HttpServletResponse response) {
        List<ProSubjectVo> list = iProSubjectService.queryList(bo);
        ExcelUtil.exportExcel(list, "项目管理", ProSubjectVo.class, response);
    }

    /**
     * 获取项目管理详细信息
     *
     * @param subjectId 主键
     */
    @SaCheckPermission("system:subject:query")
    @GetMapping("/{subjectId}")
    public R<ProSubjectVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long subjectId) {
        return R.ok(iProSubjectService.queryById(subjectId));
    }

    /**
     * 新增项目管理
     */
    @SaCheckPermission("system:subject:add")
    @Log(title = "项目管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ProSubjectBo bo) {
        return toAjax(iProSubjectService.insertByBo(bo));
    }

    /**
     * 修改项目管理
     */
    @SaCheckPermission("system:subject:edit")
    @Log(title = "项目管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ProSubjectBo bo) {
        return toAjax(iProSubjectService.updateByBo(bo));
    }

    /**
     * 删除项目管理
     *
     * @param subjectIds 主键串
     */
    @SaCheckPermission("system:subject:remove")
    @Log(title = "项目管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{subjectIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] subjectIds) {
        return toAjax(iProSubjectService.deleteWithValidByIds(Arrays.asList(subjectIds), true));
    }

    /**
     * 根据模板生成项目管理
     *
     * @param subjectId 主键串
     */
    @Log(title = "项目管理", businessType = BusinessType.DELETE)
    @GetMapping("/generateByTemplate")
    public R<Void> generateByTemplate(@RequestParam Long subjectId) {
        return toAjax(iProSubjectService.generateByTemplate(subjectId));
    }
}
